package com.neusmart.common.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.inputmethod.InputMethodManager;

import com.baidu.mobstat.StatService;
import com.neusmart.common.R;
import com.neusmart.common.dialog.CustomLoadingDialog;
import com.neusmart.common.dialog.HintDialog;
import com.neusmart.common.dialog.NewAlertDialog;
import com.neusmart.common.dialog.NewAlertDialogWithTitle;
import com.neusmart.common.dialog.OnHintListener;
import com.neusmart.common.dialog.OnNewClickListener;
import com.neusmart.common.util.ActivityUtil;
import com.neusmart.common.util.MToast;
import com.neusmart.common.util.NetworkUtil;
import com.neusmart.common.view.MyToast;

/**
 * Base Activity
 * <p/>
 * roy
 */
public abstract class BaseActivity extends FragmentActivity implements DialogInterface.OnCancelListener {

    private boolean currentNetworkConnected = true;
    //
    private CustomLoadingDialog loadingDialog;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean connected = isNetworkConnected();
            if (!ActivityUtil.isBackgroundRunning(context, getPackageName())) {
                if (currentNetworkConnected) {//断开
                    if (!connected)
                        showNetworkConnection(false);
                } else {//连接
                    if (connected) {
                        showNetworkConnection(true);
                        onRefreshContent();
                    }
                }
            }
            currentNetworkConnected = connected;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver();
        setContentView(getLayoutResID());
        init();
    }

    protected abstract int getLayoutResID();

    protected abstract void init();

    protected boolean showNetworkConnection(boolean isConnected) {
        showToast(isConnected ? R.string.network_connect : R.string.network_interupt);
        return false;
    }

    protected void onRefreshContent() {
        //网络连接时，或者重新登录后，刷新页面
    }

    protected void showToast(int resId) {
        MToast.showText(this, resId);
    }

    protected void showToast(String msg) {
        MToast.showText(this, msg);
    }

    protected void showImageAndTextToast(int resId, String text) {
        MyToast.showImgAndTextToast(this, resId, text);
    }

    protected void showImageAndTextToast(int resId, int textId) {
        MyToast.showImgAndTextToast(this, resId, textId);
    }

    protected void showTextToast(String text) {
        MyToast.showTextToast(this, text);
    }

    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        if (null != receiver)
            registerReceiver(receiver, filter);
    }

    private void unregisterReceiver() {
        if (null != receiver)
            unregisterReceiver(receiver);
    }

    protected boolean isNetworkConnected() {
        return NetworkUtil.isNetworkConnected(this);
    }

    protected void showLoadingDialog() {
        loadingDialog = new CustomLoadingDialog(this);
        loadingDialog.show();
    }

    protected void showLoadingDialog(String txt) {
        loadingDialog = new CustomLoadingDialog(this);
        loadingDialog.setHintText(txt);
        loadingDialog.show();
    }

    protected void showLoadingDialog(int txtId) {
        loadingDialog = new CustomLoadingDialog(this);
        loadingDialog.setHintText(txtId);
        loadingDialog.show();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        dismissLoadingDialog();
    }

    protected void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }

    protected void showHintDialog(int resId, OnHintListener l) {
        this.showHintDialog(getString(resId), l);
    }

    protected void showHintDialog(String text, OnHintListener l) {
        HintDialog dialog = new HintDialog(this);
        dialog.setMessage(text);
        dialog.setOnHintListener(l);
        dialog.show();
    }

    protected void showNewAlertDialog(String msg, String btnLeft, String btnRight, OnNewClickListener l) {
        NewAlertDialog dialog = new NewAlertDialog(this);
        dialog.setMessage(msg);
        dialog.setLeftButton(btnLeft);
        dialog.setRightButton(btnRight);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void showNewAlertDialog(int msgId, int btnLeftId, int btnRightId, OnNewClickListener l) {
        NewAlertDialog dialog = new NewAlertDialog(this);
        dialog.setMessage(msgId);
        dialog.setLeftButton(btnLeftId);
        dialog.setRightButton(btnRightId);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void showNewAlertDialogWithTitle(String title, String msg, String btnLeft, String btnRight, OnNewClickListener l) {
        NewAlertDialogWithTitle dialog = new NewAlertDialogWithTitle(this);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setLeftButton(btnLeft);
        dialog.setRightButton(btnRight);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void showNewAlertDialogWithTitle(int titleId, int msgId, int btnLeftId, int btnRightId, OnNewClickListener l) {
        NewAlertDialogWithTitle dialog = new NewAlertDialogWithTitle(this);
        dialog.setTitle(titleId);
        dialog.setMessage(msgId);
        dialog.setLeftButton(btnLeftId);
        dialog.setRightButton(btnRightId);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void switchActivity(Class<?> cls, Bundle extras) {
        Intent intent = new Intent(this, cls);
        if (extras != null)
            intent.putExtras(extras);
        startActivity(intent);
    }

    protected void switchActivity(Class<?> cls, Bundle extras, int flags) {
        Intent intent = new Intent(this, cls);
        if (extras != null)
            intent.putExtras(extras);
        intent.setFlags(flags);
        startActivity(intent);
    }

    protected void switchActivityAndFinish(Class<?> cls, Bundle extras) {
        switchActivity(cls, extras);
        finish();
    }

    protected void switchActivityAndFinish(Class<?> cls, Bundle extras, int flags) {
        switchActivity(cls, extras, flags);
        finish();
    }

    protected void switchActivityForResult(Class<?> cls, int requestCode, Bundle extras) {
        Intent intent = new Intent(this, cls);
        if (extras != null)
            intent.putExtras(extras);
        startActivityForResult(intent, requestCode);
    }

    protected void switchActivityForResult(Class<?> cls, int requestCode, Bundle extras, int flags) {
        Intent intent = new Intent(this, cls);
        if (extras != null)
            intent.putExtras(extras);
        intent.setFlags(flags);
        startActivityForResult(intent, requestCode);
    }

    protected void finishActivityForResult() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }

    protected void finishActivityWithExtra(Bundle extras) {
        Intent intent = new Intent();
        if (extras != null)
            intent.putExtras(extras);
        setResult(RESULT_OK, intent);
        finish();
    }

    protected void switchActivityReorder2Front(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
    }

    protected void switchActivityReorder2Front(Class<?> cls, int requestCode) {
        Intent intent = new Intent(this, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
        startActivityForResult(intent, requestCode);
    }

    protected void switchFragment(Fragment fragment, int resId, Bundle extras) {
        if (extras != null)
            fragment.setArguments(extras);
        getSupportFragmentManager().beginTransaction()
                .replace(resId, fragment).commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver();
    }

    protected void hideSoftInput() {
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

}
